package com.igoal.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Type;

@Entity
@Table(name="goal_entry")
@Searchable
public class UserGoalEntry extends BaseObject implements Entry, Serializable, Audit, ItemOwner, PrivacySupport {
	private static final long serialVersionUID = -4734913561659431360L;
	
	private Long id;
	private UserGoal userGoal;
	private String title;
	private String body;
	private String imgURL;
	private boolean block;
	private Integer commentCount;
	private Integer cheerCount;	
	private Date createDate;
	private Date updateDate;
	private DataStatus status;	
	private Set<CommentEntry> comments = new HashSet<CommentEntry>();
	private PrivacySetting privacy;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@OneToMany(mappedBy="entry")
	public Set<CommentEntry> getComments() {
		return comments;
	}

	public void setComments(Set<CommentEntry> comments) {
		this.comments = comments;
	}
	
	@Transient
	public User getUser(){
		return userGoal.getUser();
	}

	@ManyToOne
	@JoinColumns({
		@JoinColumn(name="user_id", referencedColumnName="user_id"),
		@JoinColumn(name="goal_id", referencedColumnName="goal_id")
	})
	public UserGoal getUserGoal() {
		return userGoal;
	}

	public void setUserGoal(UserGoal userGoal) {
		this.userGoal = userGoal;
	}

	@Column(name="entry_body", nullable=false)
	@SearchableProperty
	public String getBody() {
		return body;
	}
	
	public void setBody(String body) {
		this.body = body;
	}

	@Column(name="img_url", nullable=true)
	public String getImgURL() {
		return imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}

	@Column(name="block", nullable=false)
	@Type(type="yes_no")
	@SearchableProperty
	public boolean isBlock() {
		return block;
	}

	public void setBlock(boolean block) {
		this.block = block;
	}
	
	@Column(name="num_comment", nullable=false)	
	public Integer getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(Integer commentCount) {
		this.commentCount = commentCount;
	}
	
	@Column(name="num_cheer", nullable=false)
	public Integer getCheerCount() {
		return cheerCount;
	}

	public void setCheerCount(Integer cheerCount) {
		this.cheerCount = cheerCount;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
            return true;
        }
        if (!(o instanceof UserGoalEntry)) {
            return false;
        }

        final UserGoalEntry entry = (UserGoalEntry) o;

        return !(title != null ? !title.equals(entry.getTitle()) : entry.getTitle() != null);
	}

	@Override
	public int hashCode() {
		return this.title.hashCode();
	}

	@Override
	public String toString() {
		return this.title;
	}

	@Column(nullable=false, name="entry_title")
	@SearchableProperty
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	@Transient
	public User getOwner() {
		return userGoal.getUser();
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "privacy")
	public PrivacySetting getPrivacy() {
		return this.privacy;
	}

	public void setPrivacy(PrivacySetting privacy) {
		this.privacy = privacy;
	}
}
